package com.matrix.draft.juc.day7;

import lombok.Data;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;

public class TestForkJoin {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ForkJoinPool pool = new ForkJoinPool(4);
        Integer result = pool.invoke(new MyTask(5));
        System.out.println(result);
    }
}

// 1~n整数求和
@Data
class MyTask extends RecursiveTask<Integer>{

    private int n;

    public MyTask(int n){
        setN(n);
    }

    @Override
    protected Integer compute() {
        if(1 == n){
            return 1;
        }
        MyTask mt = new MyTask(n - 1);
        mt.fork();
        int result = mt.join() + n;
        return result;
    }
}
